/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cnab.local.bradesco;

import java.io.File;
import java.util.ArrayList;

/**
 * @author marcio.endo@objectos.com.br (Marcio Endo)
 * @author MVCapital Modificado em 14/02/2014
 */
public class ReaderRetornoBradesco 
{
  private ArrayList<FileCNABBradesco> filesCNAB = new ArrayList<FileCNABBradesco>();
  private ArrayList<File> listFiles = new ArrayList<File>(); 
  private static String rootOriginPath = "F:\\FIDC\\LEGO_82075\\Temp";
  private static String rootDestinyPath = "F:\\FIDC\\LEGO_82075\\Temp";

  public static void main(String[] args)  
  {
    ReaderRetornoBradesco reader = new ReaderRetornoBradesco(ReaderRetornoBradesco.rootOriginPath);
    reader.writeFilesCNAB();
    reader.moveFilesToProcessed();
  }
  
  public ReaderRetornoBradesco()
  {
    
  }

  public ReaderRetornoBradesco(String rootOriginPath)
  {
    //this.rootOriginPath = rootOriginPath;
    ArrayList<File> listTempFiles = filesInDirectory(getRootOriginPath());
    for(File file:listTempFiles)
    {
      String extensionFile = file.getName().substring(file.getName().length()-3,file.getName().length());
      System.out.println("File " + file.getName() + " extension " + extensionFile);
      if (file.getName().substring(file.getName().length()-3,file.getName().length()).equals("TXT"))
      {
        this.filesCNAB.add(new FileCNABBradesco(file));
        this.listFiles.add(file);
      }
    }
    
  }
  
  public ReaderRetornoBradesco(ArrayList<FileCNABBradesco> filesCNAB)
  {
    this.setFilesCNAB(filesCNAB);
  }

  public void writeFilesCNAB()
  {
    for(FileCNABBradesco fileCNAB:this.filesCNAB)
    {
      String outputFilename = rootOriginPath + fileCNAB.getNameCSV();
      FileCNABBradesco.writeCsvFile(outputFilename, fileCNAB.getLotes());
    }        
  }
  
  public void moveFilesToProcessed()
  {
    for(File file:this.listFiles)
    {
      moveFile(file,ReaderRetornoBradesco.rootDestinyPath);
    }
  }
  
  public static void moveFile(File source, String outputFolder)
  {
    try 
    {
      File oldFile = source;
      System.out.println("Moving to " + outputFolder + oldFile.getName());
      if (oldFile.renameTo(new File(outputFolder + oldFile.getName()))) 
      {
        System.out.println("The file was moved successfully to the new folder");
      } 
      else 
      {
        System.out.println("The File was not moved.");
      }
    } 
    catch (Exception e) 
    {
      e.printStackTrace();
    }
  }

  public static ArrayList<File> filesInDirectory(String directory)
  {
    ArrayList<File> filesList = new ArrayList<File>();
    File folder = new File(directory);
    File[] listOfFiles = folder.listFiles();
    
    if (folder.listFiles()==null)
    {
      return null;
    }
    else
    {
      for (int i = 0; i < listOfFiles.length; i++) 
      {
        filesList.add(listOfFiles[i]);
      }
      return filesList;
    }
  }
  public ArrayList<FileCNABBradesco> getFilesCNAB() 
  {
    return filesCNAB;
  }

  public void setFilesCNAB(ArrayList<FileCNABBradesco> filesCNAB) 
  {
    this.filesCNAB = filesCNAB;
  }

  public String getRootOriginPath() 
  {
    return rootOriginPath;
  }

  public void setRootOriginPath(String rootOriginPath) 
  {
    //this.rootOriginPath = rootOriginPath;
  }

  public String getRootDestinyPath() 
  {
    return rootDestinyPath;
  }

  public void setRootDestinyPath(String rootDestinyPath) 
  {
    //this.rootDestinyPath = rootDestinyPath;
  }

  
}
